<template>
    <div class="login-container">
        <el-form :model="loginForm" label-width="80px" :rules="rules" ref="form">
            <el-form-item label="手机号" prop="mobile" >
                <el-input v-model="loginForm.mobile" placeholder="请输入手机号" /> 
            </el-form-item>
            <el-form-item label="密码" prop="password" >
                <el-input v-model="loginForm.password" placeholder="请输入密码" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="login">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name: 'Login',
    data() {
        return {
            loginForm: {
                mobile: '',
                password: ''
            },
            rules: {
                mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            }
        }
    },
    methods: {
        async login() {
            
            console.log(this.loginForm);
            if (this.loginForm.mobile === '') {
                this.$message.error('请输入手机号');
                return;
            }
            if (this.loginForm.password === '') {
                this.$message.error('请输入密码');
                return;
            }

            const res = await this.$axios.post('/login', this.loginForm);
            // console.log("登录请求返回的数据是：",res);
            if (res.code !== 1) {
                this.$message.error(res.message);
                return;
            }
            // console.log(res.userInfo,res.token);
            this.$store.commit('login', {
                userInfo: res.userInfo,
                token: res.token
            });
            this.$router.push('/home/welcome');
        }
    }
}
</script>
<style scoped lang="scss">
    .login-container{
    width: 100%;
    height: 100vh;
    background-color: #119ce1;
    display: flex;
    justify-content: center;
    align-items: center;
}
.el-form{
    width: 450px;
    height: 400px;
    background-color: #ffffff;
    padding: 20px;
    text-align: center;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    justify-content:center;
    align-items: center;
    &-item{
        width: 400px;
        margin-bottom: 40px;
    }
}
.el-button{
    width: 200px;
}
</style>